package com.yystudy.system.controller;


import com.yystudy.common.result.Result;
import com.yystudy.model.system.SysMenu;
import com.yystudy.model.vo.AssginMenuVo;
import com.yystudy.system.annotation.Log;
import com.yystudy.system.enums.BusinessType;
import com.yystudy.system.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author yystudy
 * @since 2023-10-13
 */
@RestController
@Api(tags = "菜单管理接口")
@RequestMapping("/admin/system/sysMenu")
public class SysMenuController {
    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 根据某个角色id获取它能操作的菜单列表
     * @param roleId
     * @return
     */
    @ApiOperation(value = "根据角色获取菜单")
    @GetMapping("/toAssign/{roleId}")
    public Result toAssign(@PathVariable Long roleId) {
        List<SysMenu> list = sysMenuService.findSysMenuByRoleId(roleId);
        return Result.ok(list);
    }

    /**
     * 给一个角色分配新的菜单列表权限
     * @param assginMenuVo
     * @return
     */
    @Log(title = "菜单权限",businessType = BusinessType.ASSGIN)
    @ApiOperation(value = "给角色分配权限")
    @PreAuthorize("hasAuthority('bnt.sysRole.assignAuth')")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginMenuVo assginMenuVo) {
        sysMenuService.doAssign(assginMenuVo);
        return Result.ok();
    }


    /**
     * 返回菜单列表
     * @return
     */
    @ApiOperation(value = "菜单列表")
    @GetMapping("/findNodes")
    @PreAuthorize("hasAuthority('bnt.sysMenu.list')")
    public Result findNodes() {
        List<SysMenu> list = sysMenuService.findNodes();
        return Result.ok(list);
    }


    /**
     * 添加菜单
     * @param sysMenu
     * @return
     */
    @PreAuthorize("hasAuthority('bnt.sysMenu.add')")
    @Log(title = "菜单权限",businessType = BusinessType.INSERT)
    @ApiOperation(value = "添加菜单")
    @PostMapping("/save")
    public Result save(@RequestBody SysMenu sysMenu){
        sysMenuService.save(sysMenu);
        return Result.ok();
    }


    /**
     * 根据id进行查询
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id进行查询")
    @GetMapping("/findNode/{id}")
    @PreAuthorize("hasAuthority('bnt.sysMenu.list')")
    public Result findNode(@PathVariable Long id) {
        SysMenu byId = sysMenuService.getById(id);
        return Result.ok(byId);
    }


    /**
     * 修改更新数据
     * @param sysMenu
     * @return
     */
    @Log(title = "菜单权限",businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改更新数据")
    @PreAuthorize("hasAuthority('bnt.sysMenu.update')")
    @PutMapping("/update")
    public Result update(@RequestBody SysMenu sysMenu){
        sysMenuService.updateById(sysMenu);
        return Result.ok();
    }


    /**
     * 根据id删除菜单
     * @param id
     * @return
     */
    @Log(title = "菜单权限",businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('bnt.sysMenu.remove')")
    @ApiOperation(value = "根据id删除菜单")
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Long id) {
        sysMenuService.removeMenuById(id);
        return Result.ok();
    }



}

